<template>
  <div id="app">
      <product v-for="(item,index) in list" :key="index"
        :name="item.name"
        :price="item.price"
        :num="item.num"
        :id="item.id"
      ></product>
      <!-- <child :num="num" @add="add"></child> -->
  </div>
</template>

<script>
import child from './components/child'
import product from './components/product'
export default {
  name: 'App',
  components: {
    child,
    product
  },
  created(){
      this.$bus.$on('add',(num,id) => {
          let index = this.list.findIndex(item => item.id === id);
          this.list[index].num = num;
      })
  },
  data(){
      return {
          num:1,
          list:[
              {
                  "name":"商品一",
                  "price":100,
                  "num":0,
                  "id":0
              },
              {
                  "name":"商品二",
                  "price":100,
                  "num":0,
                  "id":1
              },
              {
                  "name":"商品三",
                  "price":100,
                  "num":0,
                  "id":3
              }
          ]
      }
  },
  methods:{
      add(num){
          this.num = num;
      }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
